package com.lge.pitchingcoins.util.sessions;

import android.content.Context;

import com.lge.pitchingcoins.ui.clients.layer.GamePlayForClientsLayer;
import com.lge.pitchingcoins.util.PitchingCoinsLog;
import com.lge.pitchingcoins.util.Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientsSession {
    private Context mContext = null;
    
    private GamePlayForClientsLayer callback = null;

    private BufferedReader reader = null;
    private PrintWriter writer = null;
    private Socket socket = null;

    // Constructors
    public ClientsSession(Context context, String ipAddress, int port) {
        mContext = context;

        NetworkSetupThread thread = new NetworkSetupThread(ipAddress, port);
        thread.start();
    }
    
    
    // Getters and Setters
    public GamePlayForClientsLayer getCallback() {
        return callback;
    }

    public void setCallback(GamePlayForClientsLayer callback) {
        this.callback = callback;
    }    

    
    // Member Methods
    public void sendMessageToServer(String message) {
        Util.lockWithWIFI(mContext);
        PitchingCoinsLog.d("sendMessageToServer: " + message);
        try {
            if (writer != null) {
                writer.println(message);
                writer.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        Util.unlockWithWIFI(mContext);
    }


    // Inner Classes
    class NetworkSetupThread extends Thread {
        private String ipAddress = null;
        private int port = -1;

        public NetworkSetupThread(String ipAddress, int port) {
            this.ipAddress = ipAddress;
            this.port = port;
        }

        @Override
        public void run() {
            try {
                socket = new Socket(ipAddress, port);
                InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());

                reader = new BufferedReader(inputStreamReader);
                writer = new PrintWriter(socket.getOutputStream());

                Thread readerThread = new Thread(new IncomingReader());
                readerThread.start();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class IncomingReader implements Runnable {
        String message;

        @Override
        public void run() {
            try {
                Util.lockWithWIFI(mContext);

                while ((message = reader.readLine()) != null) {
                    if (callback != null) {
                        callback.onMessageReceived(message);
                    }
                }
                
                Util.lockWithWIFI(mContext);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}